﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SolveCranks.CApSCSolveCranksLogWriter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SolveCranks.Resources;
using EngineeringInternalExtension;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SolveCranks;

public class CApSCSolveCranksLogWriter : IDisposable
{
  private readonly TxReportWriterEx _writer;
  private bool _isDisposed;
  private bool _isClosed;

  public CApSCSolveCranksLogWriter(string reportFile)
  {
    this._writer = new TxReportWriterEx(reportFile);
  }

  public void Dispose()
  {
    this.Dispose(true);
    GC.SuppressFinalize((object) this);
  }

  protected virtual void Dispose(bool disposing)
  {
    if (!this._isDisposed & disposing)
      this._writer.Dispose();
    this._isDisposed = true;
  }

  public void Close()
  {
    if (this._isClosed)
      return;
    this._writer.Close();
    this._isClosed = true;
  }

  public void Show(string caption, string message, MessageBoxIcon msgIcon)
  {
    if (!this._isClosed)
      this.Close();
    if (TxMessageBox.Show(message, caption, MessageBoxButtons.YesNo, msgIcon) != DialogResult.Yes)
      return;
    string reportFileFullPath = this._writer.ReportFileFullPath;
    if (!new FileInfo(reportFileFullPath).Exists)
      return;
    Process.Start(reportFileFullPath);
  }

  public void WriteLine(string text) => this._writer.WriteLine(text);

  public void WriteEmptyLine() => this._writer.WriteLine(string.Empty);

  public void WriteLineWithCurrentTime(string text) => this._writer.WriteLineWithCurrentTime(text);

  public void WriteAlgorithmResult(CApSCSolveCranksAlgorithm.Result result)
  {
    switch (result.Status)
    {
      case CApSCSolveCranksAlgorithm.Result.eStatus.Unknown:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogUnknownError, (object) ((ITxObject) result.Object).Name));
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.AlreadyOK:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogAlreadyOK, (object) ((ITxObject) result.Object).Name));
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.Success:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogSuccess, (object) ((ITxObject) result.Object).Name));
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.CannotOpenForKinematicsModeling:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogCannotOpenForModeling, (object) ((ITxObject) result.Object).Name));
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.Failiure:
        this.WriteFailiureResult(result);
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.EndModelingFailed:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogEndModelingFailed, (object) ((ITxObject) result.Object).Name));
        break;
      case CApSCSolveCranksAlgorithm.Result.eStatus.ReloadComponentFailed:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogReloadComponentFailed, (object) ((ITxObject) result.Object).Name));
        break;
    }
  }

  public int WriteAlgorithmResultWarnings(CApSCSolveCranksAlgorithm.Result result)
  {
    int num = 0;
    foreach (CApSCSolveCranksAlgorithm.Warning warning in result.Warnings)
    {
      switch (warning.Code)
      {
        case "APP_SOLVE_CRANKS_RENAMED_JOINT":
          this.WriteLine(string.Format(CApSCSolveCranksResources.LogJointWasRenamed, (object) ((ITxObject) result.Object).Name, (object) warning.Params[0], (object) warning.Params[1]));
          ++num;
          continue;
        case "APP_SOLVE_CRANKS_DRIVING_JOINT_CHOSEN_BY_ALPHABETIC_ORDER":
          this.WriteLine(string.Format(CApSCSolveCranksResources.LogDrivingJointWasDefinedByAlphabeticOrder, (object) ((ITxObject) result.Object).Name, (object) warning.Params[0]));
          ++num;
          continue;
        default:
          continue;
      }
    }
    return num;
  }

  public void WriteSection(CApSCSolveCranksLogWriter.eSection section, params string[] parameters)
  {
    switch (section)
    {
      case CApSCSolveCranksLogWriter.eSection.SuccessfullActions:
        this.WriteLine(CApSCSolveCranksResources.LogSectionSuccessfullActions);
        break;
      case CApSCSolveCranksLogWriter.eSection.Warnings:
        this.WriteLine(CApSCSolveCranksResources.LogSectionWarnings);
        break;
      case CApSCSolveCranksLogWriter.eSection.Errors:
        this.WriteLine(CApSCSolveCranksResources.LogSectionErrors);
        break;
      case CApSCSolveCranksLogWriter.eSection.Final:
        this.WriteLine(CApSCSolveCranksResources.LogSectionFinal);
        break;
      case CApSCSolveCranksLogWriter.eSection.Summary:
        this.WriteLine(string.Format(CApSCSolveCranksResources.LogSectionSummary, (object) parameters[0], (object) parameters[1], (object) parameters[2]));
        break;
    }
  }

  private void WriteFailiureResult(CApSCSolveCranksAlgorithm.Result result)
  {
    if (string.IsNullOrEmpty(result.ErrorCode))
    {
      this.WriteLine(string.Format(CApSCSolveCranksResources.LogFailiure, (object) ((ITxObject) result.Object).Name));
    }
    else
    {
      string format;
      switch (result.ErrorCode)
      {
        case "APP_CRANK_ERR_ANGLES":
          format = CApSCSolveCranksResources.LogFailiureCalculateCrankAngles;
          break;
        case "APP_CRANK_ERR_JOINTS_DATA":
          format = CApSCSolveCranksResources.LogFailiureCreateCrankJoints;
          break;
        case "APP_CRANK_ERR_JOINT_FUNC":
          format = CApSCSolveCranksResources.LogFailiureSetCrankJointsFunctions;
          break;
        case "APP_CRANK_ERR_LINKS_DATA":
          format = CApSCSolveCranksResources.LogFailiureCreateCrankLinks;
          break;
        case "APP_CRANK_ERR_PLANE":
          format = CApSCSolveCranksResources.LogFailiureCrankPlane;
          break;
        case "APP_CRANK_ERR_SIDE_LENGTHS":
          format = CApSCSolveCranksResources.LogFailiureCalculateCrankJointDistances;
          break;
        case "CAPSCSOLVECRANKSALGORITHM_DEVICE_COMPILATION_VERIFICATION_ERROR":
          format = CApSCSolveCranksResources.LogFailiureDeviceCompilation;
          break;
        default:
          format = CApSCSolveCranksResources.LogFailiure;
          break;
      }
      this.WriteLine(string.Format(format, (object) ((ITxObject) result.Object).Name));
    }
  }

  public enum eSection
  {
    SuccessfullActions,
    Warnings,
    Errors,
    Final,
    Summary,
  }
}
